<?php

namespace App\Services\Admin;

use App\Exceptions\LogicException;
use App\Models\Logs;

class LogsService
{
    protected $logsModel;

    public function __construct(Logs $logsModel)
    {
        $this->logsModel = $logsModel;
    }

    /**
     * 获取日志列表
     * @param array $params 查询参数
     * @return array
     */
    public function list(array $params): array
    {
        $fields = [
            'id',
            'admin_id',
            'event_name',
            'content',
            'url',
            'ip',
            'method',
            'module',
            'action',
            'log_type',
            'status_code',
            'response_time',
            'created_at'
        ];
        return $this->logsModel->list($params, $fields);
    }

    /**
     * 获取日志详情
     * @param int $id 日志ID
     * @return array
     * @throws LogicException
     */
    public function view(int $id): array
    {
        $fields = [
            'id',
            'admin_id',
            'event_name',
            'content',
            'url',
            'query',
            'params',
            'response',
            'ip',
            'user_agent',
            'method',
            'module',
            'action',
            'log_type',
            'status_code',
            'response_time',
            'created_at'
        ];
        $log = $this->logsModel->take(['id' => $id], $fields);
        if (empty($log)) {
            throw new LogicException('日志不存在');
        }
        return $log;
    }

    /**
     * 创建日志
     * @param array $data 日志数据
     * @param int $adminId 管理员ID
     * @return array
     * @throws LogicException
     */
    public function create(array $data, int $adminId = 0): array
    {
        // 设置默认值
        $data['admin_id'] = $adminId;
        $data['log_type'] = $data['log_type'] ?? Logs::LOG_TYPE_OPERATION;

        $id = $this->logsModel->create($data, time());
        return ['id' => $id];
    }

    /**
     * 更新日志
     * @param int $id 日志ID
     * @param array $data 更新数据
     * @param int $adminId 当前操作用户ID
     * @return array
     * @throws LogicException
     */
    public function update(int $id, array $data, int $adminId = 0): array
    {
        if (!$this->logsModel->take(['id' => $id], ['id'])) {
            throw new LogicException('日志不存在');
        }

        $data['updated_at'] = time();
        $rows = $this->logsModel->modify($id, $data);
        return ['affected_rows' => $rows];
    }

    /**
     * 删除日志
     * @param int $id 日志ID
     * @return array
     * @throws LogicException
     */
    public function delete(int $id): array
    {
        if (!$this->logsModel->take(['id' => $id], ['id'])) {
            throw new LogicException('日志不存在');
        }
        $rows = $this->logsModel->remove($id);
        return ['affected_rows' => $rows];
    }

    /**
     * 批量删除日志
     * @param array $params 删除条件
     * @return array
     */
    public function batchDelete(array $params): array
    {
        $rows = $this->logsModel->clear($params);
        return ['affected_rows' => $rows];
    }
}
